package org.sky.axis2.javacomplextype;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class PersonServiceClient {
	private static EndpointReference targetEPR = new EndpointReference(
			"http://localhost:8080/Axis2Service/services/PersonService");

	public void getPersons() {
		Options options = new Options();
		options.setAction("urn:getPersons");
		options.setTo(targetEPR);
		options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
		options.setUseSeparateListener(true);
		ServiceClient sender = null;
		try {
			PersonCallBack callback = new PersonCallBack();
			sender = new ServiceClient();
			sender.setOptions(options);
			sender.engageModule(Constants.MODULE_ADDRESSING);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace omNs = fac.createOMNamespace(
					"http://javacomplextype.axis2.sky.org", "");
			OMElement method = fac.createOMElement("getPersons", omNs);
			OMElement person = fac.createOMElement("person", omNs);
			person.setText("");
			method.addChild(method);
			sender.sendReceiveNonBlocking(method, callback);
			synchronized (callback) {
				try {
					callback.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			System.out.println("-------Error Occured-------");
			e.printStackTrace();
		} finally {
			try {
				sender.cleanup();
			} catch (Exception e) {
			}
		}
	}

	public static void main(String[] args) {
		PersonServiceClient testClient = new PersonServiceClient();
		testClient.getPersons();

	}
}